package com.app.mediaread;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private ContentResolver resolver;
    private List<Media> list;
    private ListView listView;
    private TextView musicId;
    private TextView musicName;
    private TextView musicArtist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MainActivity.this,MusicServiceActivity.class);
                intent.putExtra("Title",musicName.getText().toString());
                startActivity(intent);
            }
        });
    }
    private void initView() {
        listView = findViewById(R.id.list_music);
    }

    public void Read(View view) {
        resolver = getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]
                        {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST},
                null, null, null);
        list = new ArrayList<>();
        while (cursor.moveToNext()) {
            Media media = new Media();
            media.setId(cursor.getString(0));
            Log.e(TAG, "Read: " + cursor.getString(0));
            media.setTitle(cursor.getString(1));
            media.setArtist(cursor.getString(2));
            list.add(media);
        }
        cursor.close();
        listView.setAdapter(new MyAdapter(MainActivity.this, list));
        Log.e(TAG, "Read: " + listView);
        Toast.makeText(this, "已读", Toast.LENGTH_SHORT).show();
    }
    class MyAdapter extends BaseAdapter {

        private Context context;
        private List<Media> musicList;

        public MyAdapter(Context context, List<Media> musicList) {
            this.context = context;
            this.musicList = musicList;
        }

        @Override
        public int getCount() {
            return musicList.size();
        }

        @Override
        public Object getItem(int position) {
            return musicList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = View.inflate(context, R.layout.item_music, null);
            musicId = convertView.findViewById(R.id.music_id_tv);
            musicId.setText(musicList.get(position).getId());
            musicName = convertView.findViewById(R.id.music_name_tv);
            musicName.setText(musicList.get(position).getTitle());
            musicArtist = convertView.findViewById(R.id.music_artist_tv);
            musicArtist.setText(musicList.get(position).getArtist());
            return convertView;
        }
    }

}
